package cn.cds.withyou.lib.ui.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import cn.cds.withyou.lib.AppImageLoaderListener;
import cn.cds.withyou.lib.image.ImageListener;
import cn.cds.withyou.lib.image.ImageLoader;

public class NetworkImageView extends ImageView {

    protected String mUrl;
    protected ImageLoader mImageLoader;


    public NetworkImageView(Context context) {
        this(context, null);
    }

    public NetworkImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NetworkImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 加载网络图片
     * @param url
     * @param defaultid
     */
    public void setImageUrl(String url, int defaultid) {
        initView(url);
        loadImage(url, defaultid, false);
    }

    public void setImageUrl(String url, Drawable defaultDrawable) {
        initView(url);
        loadImage(url, defaultDrawable);
    }

    public void setImageUrl(String url, Drawable defaultDrawable, ImageListener listener){
        initView(url);
        loadImage(url, defaultDrawable, listener);
    }

    private void initView(String url){
        mUrl = url;
        if (mImageLoader == null && getContext().getApplicationContext() instanceof AppImageLoaderListener) {
            mImageLoader = ((AppImageLoaderListener) getContext().getApplicationContext()).getImageLoader();
        }
    }

    public void loadImage(String url, Drawable drawable) {
        initView(url);
        mImageLoader.loadAndShowImage(mUrl, this, drawable, null);
    }

    public void loadImage(String url, int defaultid, boolean needRound) {
        initView(url);
        mImageLoader.loadAndShowImage(mUrl, this, needRound, defaultid, null);

    }

    public void loadImage(String url, Drawable drawable, ImageListener listener){
        initView(url);
        mImageLoader.loadAndShowImage(mUrl, this, drawable, listener);
    }


}
